#include "command.h"
#include <cstring>
#include <QTime>
#include <QDateTime>
#include <QDate>
#include <QDebug>
#include <QTextStream>
#include <cstring>
#include <arpa/inet.h>

//crc code
static unsigned char auchCRCHi[] = {
    0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,
    0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
    0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01,
    0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
    0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81,
    0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
    0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01,
    0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
    0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,
    0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
    0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01,
    0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
    0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,
    0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
    0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01,
    0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
    0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,
    0x40
};

static unsigned char auchCRCLo[] = {
    0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7, 0x05, 0xC5, 0xC4,
    0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09,
    0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 0xDE, 0xDF, 0x1F, 0xDD,
    0x1D, 0x1C, 0xDC, 0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3,
    0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 0xF2, 0x32, 0x36, 0xF6, 0xF7,
    0x37, 0xF5, 0x35, 0x34, 0xF4, 0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A,
    0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29, 0xEB, 0x2B, 0x2A, 0xEA, 0xEE,
    0x2E, 0x2F, 0xEF, 0x2D, 0xED, 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26,
    0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60, 0x61, 0xA1, 0x63, 0xA3, 0xA2,
    0x62, 0x66, 0xA6, 0xA7, 0x67, 0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F,
    0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68, 0x78, 0xB8, 0xB9, 0x79, 0xBB,
    0x7B, 0x7A, 0xBA, 0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,
    0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0, 0x50, 0x90, 0x91,
    0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C,
    0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98, 0x88,
    0x48, 0x49, 0x89, 0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
    0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83, 0x41, 0x81, 0x80,
    0x40
};



u16 Command::SDI_CRC16(unsigned char *puchMsg, unsigned short usDataLen)
{
    unsigned char uchCRCHi = 0xFF ; /* 初始化高字节*/
    unsigned char uchCRCLo = 0xFF ; /* 初始化低字节*/
    unsigned int uIndex ; /*把CRC表*/
    while(usDataLen--)
    {
        uIndex = uchCRCHi ^ *puchMsg++ ; /*计算CRC */
        uchCRCHi = uchCRCLo ^ auchCRCHi[uIndex] ;
        uchCRCLo = auchCRCLo[uIndex] ;
    }
    u16 crc_data=(uchCRCLo << 8 | uchCRCHi);
    //reverse data
    crc_data=htons(crc_data);
    return crc_data ;
}


Command::Command()
{
    this->count=0;
}

void Command::resolve_from_rawdata()
{
    // qDebug()<<"resovle start";
    UCHAR* p;

    p=static_cast<UCHAR*>(this->rawdata);
    UCHAR* sp=p;  //start pointer

    memcpy(this->frame_header,p,18);  //header

    p+=6; //fix head  6 bytes
    fun_code=*(unsigned short*)p;
    p+=2;  //fun code

    //transform from netowork bytes to hosts
    fun_code=ntohs(fun_code);

    p+=2;  //communication times
    p+=6; //reserve time;

    //  possible change
    frame_length=*(unsigned short*)p;

    p+=2;  // data length
    p+=2;  // sn_fun
    p+=2; // 2nd_code_len
    memcpy(this->sn,p,6);  //get sn number;

    p+=6; //move 2 sn end
    p+=5; // move 2 start seg end;

    if(FUN_CODE_REGISTER==fun_code) // if it is regiter frame contine
    {
        memcpy(this->product_key,p,10);  //copy product key
        p+=10;   //move 2 product_key end
        memcpy(this->cust_sn,p,10);  //copy cust sn
        p+=10;
        memcpy(this->device_sn,p,10);  //copy dev sn
        p+=10;
        memcpy(this->imei,p,19);  //copy imei
        p+=19;
        memcpy(this->iccid,p,24);
        p+=24;
        memcpy(this->sw_version,p,8);
        p+=8;
        memcpy(this->upgrade_flag,p,5);
        p+=5;

        p+=5;  //sign end  0006000103
        memcpy(this->crc_full,p,6);

    }
    if(FUN_DATA_UPLOAD==fun_code) // if it is gps data frame contine
    {
        memcpy(this->station_latitude,p,16);
        p+=16;
        memcpy(this->station_longitude,p,16);
        p+=16;
        memcpy(this->gps_latitude,p,16);
        p+=16;
        memcpy(this->gps_longitude,p,16);
        p+=16;
        memcpy(this->battery_vol,p,14);
        p+=14;
        memcpy(this->singal_intensity,p,8);
        p+=8;
        memcpy(this->running_time,p,7);
        p+=7;
        memcpy(this->vabirant,p,5);
        p+=5;

        p+=5;  //sign end  0006000103
        memcpy(this->crc_full,p,6);
    }

    // get configure code if needed
    if(FUN_RETURN_CONFIGURE==fun_code)
    {
        p+=5;  //move 2 return code (0xf017) end
        memcpy(&config_cod,p,2);
    }
    qDebug()<<"fun="<<fun_code<<"frame_length="<<frame_length;
    qDebug()<<"resovle end";
}

void Command::transform_to_rawdata()
{

}


void Command::register_sync_time()
{
    UCHAR gps_format[MAX_BUF_LENGTH]={
        0x7E,0x7E,0x23,0x23,0x55,0x54,0x4E,0x11,0x00,0x11,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3e,
        0x00,0x01,0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x05,0x00,0x01,0x02,
        0x00,0x04,0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x08,0x00,0x04,0x00,0x00,0x00,0x00,
        0x00,0x06,0x00,0x01,0x1b,
        0x00,0x07,0x00,0x02,0x1e,0x37
    };
    //registerframe  to database
    //mysql_qurey="";



    this->frame_length=0x3e;
    //qDebug()<<"frame_length command"<<frame_length;
    UCHAR* p=gps_format;
    //change SN
    p+=22;   //move 2 sn adddress
    memcpy(p,&this->sn,6);
    //qDebug()<<"data[0]"<<gps_format[0];
    //get time
    QDate now_date=QDate::currentDate();
    QTime now_time=QTime::currentTime();

    UCHAR year=(UCHAR)(now_date.year()%100);
    UCHAR month=now_date.month();
    UCHAR day=now_date.day();

    UCHAR hour=now_time.hour();
    UCHAR min=now_time.minute();
    UCHAR sec=now_time.second();

    char year_char[12];
    sprintf(year_char,"%02d%02d%02d%02d%02d%02d",sec,min,hour,day,month,year);
    QString s(year_char);
    qlonglong t=s.toLongLong(NULL,16);
    //    qDebug()<<"s="<<s<<"t="<<t;
    //    qDebug("%llx",t);
    UCHAR time_rawdata[6];
    memcpy(time_rawdata,&t,6);
    //change time
    qDebug()<<"data[0]"<<gps_format[0];
    p+=15;  //move to time address
    memcpy(p,time_rawdata,6);

    qDebug()<<"data[0]"<<gps_format[0];
    //compute and write crc
    u16 crc_code=this->SDI_CRC16(gps_format,56); // len=0x3e-6
    memcpy(gps_format+62-2,&crc_code,2);
    qDebug()<<"data[0]"<<gps_format[0];
    //copy 2 socket raw data;
    memcpy(this->rawdata,gps_format,frame_length);
    qDebug()<<"sync time  end";
}


void  Command::return_status()
{
    qDebug()<<"return status start";
    UCHAR status_format[MAX_BUF_LENGTH]={
        0x7E,0x7E,0x23,0x23,0x55,0x54,
        0x4E,0xEE,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x31,
        0x00,0x01,0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x05,0x00,0x01,0x02,
        0x00,0x0e,0x00,0x01,0x01,
        0x00,0x06,0x00,0x01,0x1b,
        0x00,0x07,0x00,0x02,0x1e,0x37  //crc
    };
    UCHAR* p=status_format;
    this->frame_length=0x31;
    //change SN
    p+=22;   //move 2 sn adddress
    memcpy(p,&this->sn,6);

    //compute and write crc
    u16 crc_code=this->SDI_CRC16(status_format,43); // len=0x31-6=49-6
    memcpy(status_format+49-2,&crc_code,2);

    //copy raw data
    memcpy(this->rawdata, status_format,frame_length);
    qDebug()<<"return status end";
}


//may have 8 command
//1.upgrade
//2.bind sn
//3.set host ip
//4.set host port
//5. modify active time
//6.open gps postion
//7. set sleep time
//8. finish
int  Command::configure(int count)
{
    // configure   procedure
    //1.get command from mysql
    qDebug()<<"configure start";
    qDebug()<<"count"<<count;

    int configure_cod=0;

    //test code
    if(1==count)
        configure_cod=CONFIGURE_BIND;
    if(2==count)
        configure_cod=CONFIGURE_GPS_POSTION;
    if(3==count)
        configure_cod=CONFIGURE_SLEEP;
    if(4==count)
        configure_cod=CONFIGURE_WORK_TIME;
    if(5<=count)
        configure_cod=CONFIGURE_FINISH;


    char configure_buf[64];

    // configure_cod= query_configure_databsee(configure_buf);

    if(CONFIGURE_UPGRADE==configure_cod)
    {
        configure_upgrade();
    }

    if(CONFIGURE_BIND==configure_cod)
    {
        configure_bind_sn(configure_buf);
    }

    if(CONFIGURE_IP==configure_cod)
    {
        configure_set_host_ip(configure_buf);
    }

    if(CONFIGURE_PORT==configure_cod)
    {
        configure_set_host_port(configure_buf);
    }

    if(CONFIGURE_GPS_POSTION==configure_cod)
    {
        configure_open_gps_position();
    }
    if(CONFIGURE_WORK_TIME==configure_cod)
    {
        configure_set_work_time(configure_buf);
    }

    if(CONFIGURE_SLEEP==configure_cod)
    {
        configure_set_sleep_interval(configure_buf);
    }
    if(CONFIGURE_FINISH==configure_cod)
    {
        configure_finish();
    }
    qDebug()<<"configure end";

    return 0;
}
int Command::query_configure_databsee(char* configure_buf)
{
    int t=0;
    int configure_code=CONFIGURE_FINISH;
    //1.query the database
    // select * from table where sn=sn and need_process;

    //2.process the  return query and copy 2 configure_buf
    memcpy(configure_buf,&t,4);
    return configure_code;
}

void Command::configure_upgrade()
{
    UCHAR upgrade_format[MAX_BUF_LENGTH]={
        0x7E,0x7E,0x23,0x23,0x55,0x54,   //start
        0x4E,0xEF,0x00,0x01,   //fun && comm count
        0x00,0x00,0x00,0x00,0x00,0x00,  0x00,0x31,   //  clock time and data len

        0x00,0x01,0x00,0x06, 0x18,0x07,0x16,0x01,0x00,0x05,   // seg no+ seg length device sn
        0x00,0x05,0x00,0x01,0x02,    //seg start

        0xF1,0x01,0x00,0x01,0x01,     //  configure_code=f101  upgrade

        0x00,0x06,0x00,0x01,0x02,    //seg end
        0x00,0x07,0x00,0x02,0x9f,0x78   //crc start+crc len+ crc data
    };
    qDebug()<<"upgrade command start";
    UCHAR* p=upgrade_format;
    this->frame_length=0x31;
    //change SN
    p+=22;   //move 2 sn adddress
    memcpy(p,&this->sn,6);

    //compute and write crc
    u16 crc_code=this->SDI_CRC16(upgrade_format,0x31-6); // len=0x31-6=49-6=43
    memcpy(upgrade_format+0x31-2,&crc_code,2);
    //copy raw data
    memcpy(this->rawdata, upgrade_format,frame_length);
    qDebug()<<"upgrade command end";
}


void Command::configure_bind_sn(const char* configure_buf)
{
    UCHAR bind_sn_format[MAX_BUF_LENGTH]={
        0x7E,0x7E,0x23,0x23,0x55,0x54,   //start
        0x4E,0xEF,0x00,0x01,   //fun && comm count
        0x00,0x00,0x00,0x00,0x00,0x00,  0x00,0x3C,   //  clock time and data len

        0x00,0x01,0x00,0x06, 0x18,0x07,0x16,0x01,0x00,0x05,   // seg no+ seg length device sn
        0x00,0x05,0x00,0x01,0x02,    //seg start

        0xF0,0x15,0x00,0x0C, //f015(configure_code bind)+len 12
        0x30,0x30,0x31,0x32,0x32,0x31,0x31,0x30,0x30,0x32,0x30,0x33,

        0x00,0x06,0x00,0x01,0x02,    //seg end
        0x00,0x07,0x00,0x02,0x9f,0x78   //crc start+crc len+ crc data
    };
    qDebug()<<"bind sn command start";
    UCHAR* p=bind_sn_format;
    this->frame_length=0x3C;
    //change SN
    p+=22;   //move 2 sn adddress
    memcpy(p,&this->sn,6);

    // copy bind data
    p=p+6+5+4;  //p+sn+seg_start+config_code
    memcpy(p,configure_buf,0x0c);

    //compute and write crc
    u16 crc_code=this->SDI_CRC16(bind_sn_format,0x3c-6); // len
    memcpy(bind_sn_format+0x3c-2,&crc_code,2);
    //copy raw data
    memcpy(this->rawdata, bind_sn_format,frame_length);
    qDebug()<<"bind sn command end";
}


void Command::configure_set_host_ip(const char* configure_buf)
{
    UCHAR set_host_ip_format[MAX_BUF_LENGTH]={
        0x7E,0x7E,0x23,0x23,0x55,0x54,   //start
        0x4E,0xEF,0x00,0x01,   //fun && comm count
        0x00,0x00,0x00,0x00,0x00,0x00,  0x00,0x40,   //  clock time and data len

        0x00,0x01,0x00,0x06, 0x18,0x07,0x16,0x01,0x00,0x05,   // seg no+ seg length device sn
        0x00,0x05,0x00,0x01,0x02,    //seg start

        0xF0,0x10,0x00,0x10,  //configure_code==f010   seg_len==0010(16)
        0x33,0x39,0x2E,0x31,0x30,0x36,0x2E,0x31,0x30,0x2E,0x31,0x31,0x30,0x00,0x00,0x00,

        0x00,0x06,0x00,0x01,0x02,    //seg end
        0x00,0x07,0x00,0x02,0x9f,0x78   //crc start+crc len+ crc data
    };
    qDebug()<<"set ip command start";
    UCHAR* p=set_host_ip_format;
    this->frame_length=0x40;
    //change SN
    p+=22;   //move 2 sn adddress
    memcpy(p,&this->sn,6);

    // copy bind data
    p=p+6+5+4;  //p+sn+seg_start+config_code
    memcpy(p,configure_buf,0x10);


    //compute and write crc
    u16 crc_code=this->SDI_CRC16(set_host_ip_format,0x40-6); // len=0x3B-6=59-6=53
    memcpy(set_host_ip_format+0x40-2,&crc_code,2);
    //copy raw data
    memcpy(this->rawdata, set_host_ip_format,frame_length);
    qDebug()<<"set ip command end";
}

void Command::configure_set_host_port(const char* configure_buf)
{
    UCHAR set_host_port_format[MAX_BUF_LENGTH]={
        0x7E,0x7E,0x23,0x23,0x55,0x54,   //start
        0x4E,0xEF,0x00,0x01,   //fun && comm count
        0x00,0x00,0x00,0x00,0x00,0x00,  0x00,0x36,   //  clock time and data len

        0x00,0x01,0x00,0x06, 0x18,0x07,0x16,0x01,0x00,0x05,   // seg no+ seg length device sn
        0x00,0x05,0x00,0x01,0x02,    //seg start
        0xF0,0x11,0x00,0x06,          //  configure_code=0xf011,  len=06
        0x38,0x38,0x38,0x38,0x00,0x00,
        0x00,0x06,0x00,0x01,0x02,    //seg end
        0x00,0x07,0x00,0x02,0x9f,0x78   //crc start+crc len+ crc data
    };
    qDebug()<<"set_host_port command start";
    UCHAR* p=set_host_port_format;
    this->frame_length=0x36;
    //change SN
    p+=22;   //move 2 sn adddress
    memcpy(p,&this->sn,6);

    // copy bind data
    p=p+6+5+4;  //p+sn+seg_start+config_code
    memcpy(p,configure_buf,0x06);

    //compute and write crc
    u16 crc_code=this->SDI_CRC16(set_host_port_format,0x36-6); // len=0x31-6=49-6=43
    memcpy(set_host_port_format+0x36-2,&crc_code,2);
    //copy raw data
    memcpy(this->rawdata, set_host_port_format,frame_length);
    qDebug()<<"set_host_port command end";
}


void Command::configure_set_work_time(const char* configure_buf)
{
    UCHAR set_work_time_format[MAX_BUF_LENGTH]={
        0x7E,0x7E,0x23,0x23,0x55,0x54,   //start
        0x4E,0xEF,0x00,0x01,   //fun && comm count
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x38,   //  clock time and data len

        0x00,0x01,0x00,0x06, 0x18,0x07,0x16,0x01,0x00,0x05,   // seg no+ seg length device sn
        0x00,0x05,0x00,0x01,0x02,    //seg start
        0xF0,0x12,0x00,0x08,         //  configure_code f012 len=08
        0x30,0x39,0x33,0x30,0x31,0x38,0x30,0x30,
        0x00,0x06,0x00,0x01,0x02,    //seg end
        0x00,0x07,0x00,0x02,0x9f,0x78   //crc start+crc len+ crc data
    };
    qDebug()<<"set work time command start";
    UCHAR* p=set_work_time_format;
    this->frame_length=0x38;
    //change SN
    p+=22;   //move 2 sn adddress
    memcpy(p,&this->sn,6);

    // copy bind data
    p=p+6+5+4;  //p+sn+seg_start+config_code
    memcpy(p,configure_buf,0x08);

    //compute and write crc
    u16 crc_code=this->SDI_CRC16(set_work_time_format,0x38-6); // len
    memcpy(set_work_time_format+0x38-2,&crc_code,2);
    //copy raw data
    memcpy(this->rawdata, set_work_time_format,frame_length);
    qDebug()<<"set work time command end";
}



void Command::configure_open_gps_position()
{
    UCHAR open_gps_position_format[MAX_BUF_LENGTH]={
        0x7E,0x7E,0x23,0x23,0x55,0x54,   //start
        0x4E,0xEF,0x00,0x01,   //fun && comm count
        0x00,0x00,0x00,0x00,0x00,0x00,  0x00,0x31,   //  clock time and data len

        0x00,0x01,0x00,0x06, 0x18,0x07,0x16,0x01,0x00,0x05,   // seg no+ seg length device sn
        0x00,0x05,0x00,0x01,0x02,    //seg start

        0xF0,0x13,0x00,0x01,0x31,     // fun code=0xf013

        0x00,0x06,0x00,0x01,0x02,    //seg end
        0x00,0x07,0x00,0x02,0x9f,0x78   //crc start+crc len+ crc data
    };
    qDebug()<<"open gps command start";
    UCHAR* p=open_gps_position_format;
    this->frame_length=0x31;
    //change SN
    p+=22;   //move 2 sn adddress
    memcpy(p,&this->sn,6);
    //compute and write crc
    u16 crc_code=this->SDI_CRC16(open_gps_position_format,0x31-6); // len=0x31-6=49-6=43
    memcpy(open_gps_position_format+0x31-2,&crc_code,2);
    //copy raw data
    memcpy(this->rawdata,open_gps_position_format,frame_length);
    qDebug()<<"open gps command end";
}

void Command::configure_set_sleep_interval(const char* configure_buf)
{
    UCHAR set_sleep_interval_format[MAX_BUF_LENGTH]={
        0x7E,0x7E,0x23,0x23,0x55,0x54,   //start
        0x4E,0xEF,0x00,0x01,   //fun && comm count
        0x00,0x00,0x00,0x00,0x00,0x00,  0x00,0x32,   //  clock time and data len

        0x00,0x01,0x00,0x06, 0x18,0x07,0x16,0x01,0x00,0x05,   // seg no+ seg length device sn
        0x00,0x05,0x00,0x01,0x02,    //seg start

        0xF0,0x14,0x00,0x02,0x04,0xb0,     // funcode=0xf014

        0x00,0x06,0x00,0x01,0x02,    //seg end
        0x00,0x07,0x00,0x02,0x9f,0x78   //crc start+crc len+ crc data
    };

    qDebug()<<"set_sleep_interval command start";
    UCHAR* p=set_sleep_interval_format;
    this->frame_length=0x32;
    //change SN
    p+=22;   //move 2 sn adddress
    memcpy(p,&this->sn,6);

    // copy bind data
    p=p+6+5+4;  //p+sn+seg_start+config_code
    memcpy(p,configure_buf,0x02);

    //compute and write crc
    u16 crc_code=this->SDI_CRC16(set_sleep_interval_format,0x32-6); // len=0x31-6=49-6=43
    memcpy(set_sleep_interval_format+0x32-2,&crc_code,2);
    //copy raw data
    memcpy(this->rawdata,set_sleep_interval_format,frame_length);
    qDebug()<<"set_sleep_interval command end";
}



void  Command::configure_finish()
{
    // qDebug()<<"return finish start";
    UCHAR finish_format[MAX_BUF_LENGTH]=
    {
        0x7E,0x7E,0x23,0x23,0x55,0x54,   //start
        0x4E,0xEF,0x00,0x01,   //fun && comm count
        0x00,0x00,0x00,0x00,0x00,0x00,  0x00,0x31,   //  clock time and data len

        0x00,0x01,0x00,0x06, 0x18,0x07,0x16,0x01,0x00,0x05,   // seg no+ seg length device sn
        0x00,0x05,0x00,0x01,0x02,    //seg start

        0xF0,0x16,0x00,0x01,0x31,     //  f016  configure,   (0x31 finish)

        0x00,0x06,0x00,0x01,0x03,    //seg end
        0x00,0x07,0x00,0x02,0x52,0x3E   //crc start+crc len+ crc data
    };
    qDebug()<<"configure finish start";
    UCHAR* p=finish_format;
    this->frame_length=0x31;
    //change SN
    p+=22;   //move 2 sn adddress
    memcpy(p,&this->sn,6);
    //compute and write crc
    u16 crc_code=this->SDI_CRC16(finish_format,0x31-6); // len=0x31-6=49-6=43
    memcpy(finish_format+0x31-2,&crc_code,2);
    //copy raw data
    memcpy(this->rawdata, finish_format,frame_length);
    qDebug()<<"configure finish  end";
}


int Command::finish_configure_database()
{
    return 0;
}

//fun to transform from char* to string
QString Command::raw_data_to_qstring(unsigned char* data,int size)
{
    QString s;
    int i=0;
    for(i=0;i<size;i++)
    {
        s+=QString::number((int)(*(data+i)),16);
    }
    return  s;
}

QString Command::register_frame_insert()
{
    //write register frame to database
    //        INSERT INTO `lvjingcleaning`.`REGISTER_FRAME`
    //        (`id`,
    //        `header`,
    //        `sn`,
    //        `sign_start`,
    //        `product_key`,
    //        `cust_sn`,
    //        `device_sn`,
    //        `imei`,
    //        `iccid`,
    //        `swvision`,
    //        `upgrade_flag`,
    //        `sign_end`,
    //        `crc`,
    //        `create_time`)
    //        VALUES
    //        (<{id: }>,
    //        <{header: }>,
    //        <{sn: }>,
    //        <{sign_start: }>,
    //        <{product_key: }>,
    //        <{cust_sn: }>,
    //        <{device_sn: }>,
    //        <{imei: }>,
    //        <{iccid: }>,
    //        <{swvision: }>,
    //        <{upgrade_flag: }>,
    //        <{sign_end: }>,
    //        <{crc: }>,
    //        <{create_time: }>);
    QString sql_qurey= " INSERT INTO `lvjingcleaning`.`REGISTER_FRAME` values ( default,'" ;// id
    sql_qurey+=raw_data_to_qstring(this->frame_header,16)+"','";//header`,
    sql_qurey+=raw_data_to_qstring(this->sn,6)+"','";//sn`,
    sql_qurey+="0005000102','";  //`sign_start`,
    sql_qurey+=raw_data_to_qstring(this->product_key,10)+"','";  //`product_key`,
    sql_qurey+=raw_data_to_qstring(this->cust_sn,10)+"','";   //cust_sn
    sql_qurey+=raw_data_to_qstring(this->device_sn,10)+"','";  //device_sn
    sql_qurey+=raw_data_to_qstring(this->imei,19)+"','";   //imei
    sql_qurey+=raw_data_to_qstring(this->iccid,24)+"','";  //iccid
    sql_qurey+=raw_data_to_qstring(this->sw_version,8)+"','";  //sw_version
    sql_qurey+=raw_data_to_qstring(this->upgrade_flag,5)+"','";  //`upgrade_flag`,
    sql_qurey+="0006000103','";    //sign end
    sql_qurey+=raw_data_to_qstring(this->crc_full,6)+"',";
    sql_qurey+="now() );"; // create_time;
    return sql_qurey;

}
